﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.SweptVolume.CApSVSweptVolumeCreationSamplingManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands.SweptVolume;

public class CApSVSweptVolumeCreationSamplingManager : CApSamplingManager
{
  private TxSweptVolumeCalculationData m_sweptVolumeCalculationData;
  private TxSweptVolume_MayProceedCalculation m_mayProceedCallback;
  private Dictionary<ITxLocatableObject, List<TxTransformation>> m_objHash;
  private Hashtable m_cablesHash;

  public CApSVSweptVolumeCreationSamplingManager(
    CApSVTimeSampleSimulationHandler simHandler,
    CApSVSamplingObjectsHandler objHandler,
    bool includeCables)
    : base((CApSimulationHandler) simHandler, objHandler, includeCables)
  {
    this.m_sweptVolumeCalculationData = new TxSweptVolumeCalculationData();
    this.m_sweptVolumeCalculationData.AutoInflateSweptVolume = TxApplication.Options.Motion.AutoInflateSweptVolumes;
    this.m_objHash = new Dictionary<ITxLocatableObject, List<TxTransformation>>();
    this.m_cablesHash = new Hashtable();
    this.m_mayProceedCallback = (TxSweptVolume_MayProceedCalculation) null;
  }

  public TxSweptVolumeCalculationData SweptVolumeCalculationData
  {
    get => this.m_sweptVolumeCalculationData;
  }

  public TxSweptVolume_MayProceedCalculation CallBack
  {
    set => this.m_mayProceedCallback = value;
  }

  protected override void sample()
  {
    if (this.m_mayProceedCallback != null)
      this.m_mayProceedCallback.Invoke(0);
    TxObjectList objectsForSampling = this.m_objHandler.getObjectsForSampling();
    bool flag = false;
    foreach (ITxObject itxObject in (Collection<ITxObject>) objectsForSampling)
    {
      if (itxObject is ITxCable)
      {
        ITxCable cable = itxObject as ITxCable;
        try
        {
          flag = flag || TxFlxCableEx.UpdateControlPoints(cable);
          this.appendCableCurPosition(cable);
        }
        catch (TxException ex)
        {
        }
      }
      else if (itxObject is ITxLocatableObject)
      {
        ITxLocatableObject key = itxObject as ITxLocatableObject;
        TxTransformation absoluteLocation = key.AbsoluteLocation;
        List<TxTransformation> txTransformationList = (List<TxTransformation>) null;
        if (this.m_objHash.TryGetValue(key, out txTransformationList))
        {
          if (txTransformationList.Count == 0 || !txTransformationList[txTransformationList.Count - 1].Equals((object) absoluteLocation))
            txTransformationList.Add(absoluteLocation);
        }
        else
          this.m_objHash.Add(key, new List<TxTransformation>(1000)
          {
            absoluteLocation
          });
      }
    }
    TxObjectList txObjectList = new TxObjectList();
    foreach (ITxLocatableObject key in this.m_objHash.Keys)
    {
      if (!((Collection<ITxObject>) objectsForSampling).Contains((ITxObject) key))
      {
        this.m_sweptVolumeCalculationData.AddObjectData(this.CreateCalculationData(key, this.m_objHash[key]));
        ((Collection<ITxObject>) txObjectList).Add((ITxObject) key);
      }
    }
    foreach (ITxLocatableObject key in (Collection<ITxObject>) txObjectList)
      this.m_objHash.Remove(key);
  }

  private TxObjectSweptVolumeCalculationData CreateCalculationData(
    ITxLocatableObject locObj,
    List<TxTransformation> locations)
  {
    return new TxObjectSweptVolumeCalculationData(locObj, locations.ToArray());
  }

  protected override void internalFinalizeSampling()
  {
    foreach (ITxLocatableObject key in this.m_objHash.Keys)
      this.m_sweptVolumeCalculationData.AddObjectData(this.CreateCalculationData(key, this.m_objHash[key]));
    foreach (TxCableSweptVolumeCalculationData volumeCalculationData in (IEnumerable) this.m_cablesHash.Values)
      this.m_sweptVolumeCalculationData.AddCableData(volumeCalculationData);
  }

  private List<TxVector> getCableControlPoints(ITxCable cable)
  {
    TxCableData cableData = cable.CableData;
    List<TxVector> cableControlPoints = new List<TxVector>();
    foreach (TxTransformation controlPointsPosition in cableData.ControlPointsPositions)
      cableControlPoints.Add(controlPointsPosition.Translation);
    return cableControlPoints;
  }

  private void appendCableCurPosition(ITxCable cable)
  {
    List<TxVector> cableControlPoints = this.getCableControlPoints(cable);
    if (!this.m_cablesHash.ContainsKey((object) cable))
      this.m_cablesHash.Add((object) cable, (object) new TxCableSweptVolumeCalculationData(cable.CableData.Radius));
    (this.m_cablesHash[(object) cable] as TxCableSweptVolumeCalculationData).AppendControlPoints(cableControlPoints);
  }
}
